package com.xinxin.o2o.service.impl;

import com.xinxin.o2o.mapper.ShopMapper;
import com.xinxin.o2o.dto.ShopExecution;
import com.xinxin.o2o.enums.ShopStateEnum;
import com.xinxin.o2o.exceptions.ShopOperationException;
import com.xinxin.o2o.model.Shop;
import com.xinxin.o2o.service.ShopService;
import com.xinxin.o2o.util.ImageUtil;
import com.xinxin.o2o.util.PageUtil;
import com.xinxin.o2o.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @author 史鑫鑫
 * @date 2019-08-13 23:07
 */
@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    ShopMapper shopMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ShopExecution addShop(Shop shop, InputStream shopImg, String fileName) throws ShopOperationException {
        // 空值判断
        if (shop == null) {
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        }
        if (shop.getShopCategory().getShopCategoryId() == null || shop.getArea().getAreaId() == null) {
            return new ShopExecution(ShopStateEnum.ILLEGAL_ARGUMENT);
        }

        try {
            // 给店铺信息赋值
            shop.setEnableStatus(0);
            shop.setCreateTime(new Date());
            shop.setUpdateTime(new Date());
            shop.setAdvice("审核中");

            // 店铺信息
            int effectedNum = shopMapper.insertShop(shop);
            if (effectedNum <= 0) {
                throw new ShopOperationException("店铺创建失败");
            } else {
                if (shopImg != null) {
                    //存储图片
                    try {
                        addShopImg(shop, shopImg, fileName);
                    } catch (IOException e) {
                        throw new ShopOperationException("addShopImg error:" + e.getMessage());
                    }
                    // 更新店铺的图片地址
                    effectedNum = shopMapper.updateShop(shop);
                    if (effectedNum <= 0) {
                        throw new ShopOperationException("更新图片地址失败");
                    }
                }
            }
        } catch (Exception e) {
            throw new ShopOperationException("addShop error:" + e.getMessage());
        }
        return new ShopExecution(ShopStateEnum.CHECK, shop);
    }

    private void addShopImg(Shop shop, InputStream shopImg, String fileName) throws IOException {
        // 获取shop图片目录的相对值路径
        String dest = PathUtil.getShopImagePath(shop.getShopId());
        String shopImgAddr = ImageUtil.generateThumbnail(shopImg, dest, fileName);
        shop.setShopImg(shopImgAddr);
    }

    @Override
    public Shop getByShopId(Long shopId) {
        return shopMapper.queryByShopId(shopId);
    }

    @Override
    public ShopExecution modifyShop(Shop shop, InputStream inputStream, String fileName) throws ShopOperationException {
        // 1.判断是否需要处理图片
        if (shop == null || shop.getShopId() == null) {
            return new ShopExecution(ShopStateEnum.NULL_SHOPID);
        } else {
            try {
                if (inputStream != null && fileName != null && !"".equals(fileName)) {
                    Shop tempShop = shopMapper.queryByShopId(shop.getShopId());
                    if (tempShop.getShopImg() != null) {
                        ImageUtil.deleteFileOrPath(tempShop.getShopImg());
                    }
                    addShopImg(shop, inputStream, fileName);
                }
                // 2. 更新店铺信息
                shop.setUpdateTime(new Date());
                int effectedNum = shopMapper.updateShop(shop);
                if (effectedNum <= 0) {
                    return new ShopExecution(ShopStateEnum.INNER_ERROR);
                } else {
                    shop = shopMapper.queryByShopId(shop.getShopId());
                    return new ShopExecution(ShopStateEnum.SUCCESS, shop);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new ShopOperationException("modifyShop error:" + e.getMessage());
            }
        }
    }

    /**
     * 返回queryShopList总数
     *
     * @param shopCondition 查询条件
     * @return 数量
     */
    @Override
    public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) {
        int rowIndex = PageUtil.calculateRowIndex(pageIndex, pageSize);
        List<Shop> shopList = shopMapper.queryShopList(shopCondition, rowIndex, pageSize);
        int count = shopMapper.queryShopCount(shopCondition);
        ShopExecution shopExecution = new ShopExecution();
        if (shopList != null) {
            shopExecution.setState(ShopStateEnum.SUCCESS.getState());
            shopExecution.setStateInfo(ShopStateEnum.SUCCESS.getStateInfo());
            shopExecution.setShopList(shopList);
            shopExecution.setCount(count);
        } else {
            shopExecution.setState(ShopStateEnum.INNER_ERROR.getState());
            shopExecution.setStateInfo(ShopStateEnum.INNER_ERROR.getStateInfo());
        }
        return shopExecution;
    }
}
